package com.cf.r1032_ak;

import java.io.*;
import java.util.Arrays;

public class a {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        in.nextToken(); int t = (int) in.nval;
        while (t-- > 0) {
            in.nextToken(); int n = (int) in.nval;
            in.nextToken(); int s = (int) in.nval;
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                in.nextToken(); arr[i] = (int) in.nval;
            }

            Arrays.sort(arr);

            if (s >= arr[0] && s <= arr[n - 1]) {
                int ans = Math.min(s - arr[0] + arr[n - 1] - arr[0], arr[n - 1] - s + arr[n - 1] - arr[0]);
                out.println(ans);
            } else if (s < arr[0]) {
                int ans = arr[n - 1] - s;
                out.println(ans);
            } else {
                int ans = s - arr[0];
                out.println(ans);
            }
        }
        out.flush();
        out.close();
        br.close();
    }
}
